<?php
namespace Home\Controller;
/**
 * 战队
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class TeamController extends HomeController {

    public function _initialize() {
        parent::_initialize();
        C('DEFAULT_THEME', 'v4');
    }

    public function index($id = 0)
    {
        $id = intval($id);
        !$id && $id = 1;// TODO 默认战队id
        $this->team = event('GameTeam')->info($id, true);
        !$this->team && $this->redirect('/');

        $teams = event('GameTeam')->lists([], 1, 2);
        $teams = array_column($teams, null, 'id');
        unset($teams[$id]);
        $this->teams = $teams;

        $this->races = $this->races($team);
        $this->newslist = event('News')->lists(['game_id' => $team['id']], 1, 2);
        $this->title = '战队';
        $this->currentTab = 'team';
        $this->display();
    }

    public function videos($id = 0)
    {
        if (IS_AJAX) {
            $this->ajaxVideos($id);
        }
        $this->index($id);
    }

    public function fans($id = 0)
    {
        if (IS_AJAX) {
            $this->ajaxFansVideos($id);
        }
        $this->index($id);
    }

    private function ajaxVideos($id = 0)
    {
        $id = intval($id);
        if (!$id) {
            $this->ajaxReturn(['state' => -2, 'data' => '参数错误']);
        }

        $members = event('GameTeam')->members($id);
        $uids = array_column($members, 'user_id');

        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $map = [
            'uid' => ['in', $uids]
        ];
        $videos = event('Video')->getBaseInfoLists($map, true, '', $p, $limit);
        $total = event('Video')->total($map);
        $data = [
            'videos' => $videos,
            'total' => $total,
        ];
        $this->ajaxReturn(['state' => 0, 'data' => $data]);
    }

    private function ajaxFansVideos($id = 0)
    {
        $id = intval($id);
        if (!$id) {
            $this->ajaxReturn(['state' => -2, 'data' => '参数错误']);
        }
        $team = event('GameTeam')->info($id, false);
        if (!$team) {
            $this->ajaxReturn(['state' => -2, 'data' => '参数错误']);
        }

        $p = I('p', 1, 'intval');
        $limit = I('limit', 10, 'intval');

        $map = [
            'tags' => $team['name']
        ];
        $videos = event('Video')->getBaseInfoLists($map, true, '', $p, $limit);
        $total = event('Video')->total($map);
        $data = [
            'videos' => $videos,
            'total' => $total,
        ];
        $this->ajaxReturn(['state' => 0, 'data' => $data]);
    }

    public function races($team = [])
    {
        $rgService = service('RaceGroup');
        $map = [
            'competitor' => $team['full_name'],
            'status' => $rgService::STATUS_PASS,
            'game_id' => $team['game_id'],
            'state' => 1,
        ];
        $race_ids = $rgService->where($map)->order("race_time desc")->limit(2)->getField('id', true);

        $races = event('RaceGroup')->format_races($race_ids, false);
        return $races;
    }
}